package com.example.sunsg.okhttptest.Manager;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by sunsg on 15/10/28.
 *
 * @ descriotion 线程管理
 */
public class ThreadPoolManager {
    private static ThreadPoolManager mInstance;

    private ThreadPoolExecutor mthreadPool;

    private ThreadPoolManager() {
        // 构造一个线程池
        mthreadPool = new ThreadPoolExecutor(2, 4, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(4),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public static ThreadPoolManager getInstance() {
        if (mInstance == null) {
            synchronized (ThreadPoolManager.class) {
                if (mInstance == null) {
                    mInstance = new ThreadPoolManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 添加并执行任务
     * @param runTask
     */
    public void addTask(Runnable runTask){
        mthreadPool.execute(runTask);
    }

    /**
     * 返回线程池
     * @return
     */
    public ThreadPoolExecutor getThreadPoolExecutor(){
        return mthreadPool;
    }



}
